$(function(){
	//评论回复框显示与隐藏
	$(document).bind("click", function(e){
		if ($(e.target).closest("#add_comment").length == 0 && $(e.target).closest("#reply_comment").length == 0) {
			$("#add_comment").hide();
			$("#reply_comment").hide();
		}
		if ($(e.target).closest("#doCommentLi").length != 0) {
			$("#add_comment").show();
		}
		if ($(e.target).closest("dt[name='doCommentDt']").length != 0) {
			$("#reply_comment").show();
		}
	});
})

//请求数据是否完成
var isComplete = true;
$(window).on( 'scroll', function() {
	if ($(window).scrollTop() >= $(document).height() - $(window).height()) {
		var page_count = $("#sync_page_count").val();
		var current_page = $("#sync_page_num").val();
		if (current_page >= page_count) {
//			layer.msg('已经到底了', {
//			time : 500
//			});
			return false;
		}
		var params = "requestType=sync";
		$("input[id^='sync_']").each(function() {
			if ($(this).attr("pname") == "pageNum") {
				params = params + "&" + $(this).attr("pname") + "=" + (parseInt($(this).val()) + 1);
			} else {
				params = params + "&" + $(this).attr("pname") + "=" + $(this).val();
			}
		});
		var url = rootPath + "/wx/share/wxShareCommentPage?shareId=" + $("#shareId").val();
		// 如果上一次请求还未完成，则本次请求忽略
		if(!isComplete) {
			return false;
		}
		// 开始请求
		isComplete = false;
		$.ajax({
			type : "post",
			url : url,
			data : params,
			datatype : "json",
			success : function(jsonObject) {
				var json = $.parseJSON(jsonObject);
				$("#sync_page_num").val(json.currentPage);
				if (json.recordList) {
					// 请求已完成
					isComplete = true;
					var commentHtml = '';
					for (var i = 0; i < json.recordList.length; i++) {
						var comment = json.recordList[i];
						/* var className = "";
							if(comment.isLiked > 0) {
								className = " current ";
							} */
						commentHtml += '<li>';
						commentHtml += '	<div class="boxs2">';
						commentHtml += '		<div class="pic">';
						commentHtml += '			<img src="' + imgRootURL + comment.commentUserHeadImage + '" onerror="this.src=\'' + rootPath + '/statics_wx/images/user_img.png\'" />';
						commentHtml += '		</div>';
						commentHtml += '		<div class="item-info">';
						commentHtml += '			<div class="boxs2_1">';
						commentHtml += '				<h2>' + comment.commentNickname + '</h2>';
						commentHtml += '				<p>' + comment.commentContent + '</p>';
						commentHtml += '			</div>';
						commentHtml += '			<div class="boxs2_2">';
						commentHtml += '				<div class="left">';
						commentHtml += '					<p>' + formatDateTime(comment.commentTime) + '</p>';
						commentHtml += '				</div>';
						commentHtml += '				<div class="right">';
						commentHtml += '					<b> <a href="javascript: doReply(' + comment.id + ', \'' + comment.commentNickname + '\');" class="replybtn" rel="' + comment.id + '">回复</a></b> ';
						if(comment.isUserLike == 1){
							commentHtml += '				<b> <a href="javascript:void(0)" class="current"><i></i><span name="commentLikeQuantity">' + comment.commentLikeQuantity + '</span></a></b>';
						}else {
							commentHtml += '				<b> <a href="javascript:void(0)" onclick="doShareCommentLike(this, ' + comment.id + ')"><i></i><span name="commentLikeQuantity">' + comment.commentLikeQuantity + '</span></a></b>';
						}
						commentHtml += '				</div>';
						commentHtml += '			</div>';
						// 加载评论回复信息
						var replyList = comment.replyList;
						if(null != replyList && replyList.length > 0) {
							commentHtml += '<div class="boxs2_3">';
							commentHtml += '	<dl>';
							for(var j = 0; j < replyList.length; j++) {
								var reply = replyList[j];
								commentHtml += '			<dt>';
								commentHtml += '				<h3>' + reply.commentNickname + '：' + reply.commentContent + '</h3>';
								commentHtml += '				<div class="boxs2_2">';
								commentHtml += '					<div class="left">';
								commentHtml += '						<p>' + formatDateTime(reply.commentTime) + '</p>';
								commentHtml += '					</div>';
								commentHtml += '					<div class="right">';
								commentHtml += '						<b> <a href="javascript: doReply(' + comment.id + ', \'' + reply.commentNickname + '\');" class="replybtn" rel="' + reply.id + '">回复</a></b> ';
								if (reply.isUserLike == 1) {
									commentHtml += '					<b> <a href="javascript:void(0)" class="current"><i></i><span name="commentLikeQuantity">' + reply.commentLikeQuantity + '</span></a></b>';
								} else {
									commentHtml += '					<b> <a href="javascript:void(0)" onclick="doShareCommentLike(this, ' + reply.id + ')"><i></i><span name="commentLikeQuantity">' + reply.commentLikeQuantity + '</span></a></b>';
								}
								commentHtml += '					</div>';
								commentHtml += '				</div>';
								commentHtml += '			</dt>';
							}
							commentHtml += '	</dl>';
							if(replyList.length > 3) {
								commentHtml += '<div class="morepart">';
								commentHtml += '	<a href="javacript:void(0)"><span class="a1">共 '  + comment.commentReply + '条回复</span>';
								commentHtml += '	<span class="a2">收起所有评论</span><i></i></a>';
								commentHtml += '</div>';
							}
							commentHtml += '</div>';
						}
						commentHtml += '		</div>';
						commentHtml += '	</div>';
						commentHtml += '</li>';
					}
					$("#commentUl").append(commentHtml);
					$('.morepart a').click(function() {
						$(this).toggleClass('current');
						$(this).parent().parent().find('dl').toggleClass('current');
					});
				}
			},
			error : function() {
				layer.msg('评论查询失败', {
					time : 1000
				});
			}
		});
	}
});


/**
 * 评论、回复
 */
function doReply(parentCommentId, nickName) {
	setTimeout(function(){
		$("#replyCommentContent").focus();
	},50);
	$("#replyCommentContent").attr("placeholder","回复："+nickName);
	$("#parentCommentId").val(parentCommentId);
	$("#parentCommentName").val(nickName);
	$("#reply_comment").show();
}

//回复评论
function publishReplyComment(this_){
	var shareId = $("#shareId").val();
	var parentCommentId = $("#parentCommentId").val();
	var parentCommentName = $("#parentCommentName").val();
	var replyCommentContent = $("#replyCommentContent").val();
	if (!replyCommentContent) {
		layer.msg('必须填写评论内容',{time:1000});
		return;
	}
	if (replyCommentContent.length > 300) {
		layer.msg('评论内容最多300字',{time:1000});
		return;
	}
	$(this_).removeAttr("onclick");
	$("#reply_comment").hide();
	$.ajax({
		type:"post",
		url:rootPath+"/wx/share/publishShareComment",
		data:{shareId: shareId, parentCommentId: parentCommentId, parentCommentName: parentCommentName, commentContent: replyCommentContent},
		datatype: "json",
		success:function(json){
			if (json.resultCode == "1") {
				layer.msg('评论成功',{time:1000});
				setTimeout(function(){
					window.location.reload();
				},1500);
			} else {
				layer.msg(json.resultMsg,{time:1000});
			}
		},
		error: function(json){
			console.log(json.resultMsg);
		}
	});
}

//评论点赞
function doShareCommentLike(this_, commentId){
	var url = rootPath + "/wx/share/doShareCommentLike";
	$.ajax({
		type : "post",
		url : url,
		data : {commentId: commentId},
		datatype : "json",
		success : function(jsonObject) {
			if (jsonObject.resultCode == '1') {
				$(this_).addClass("current");
				$(this_).closest("a").removeAttr("onclick");
				var likeNum = $(this_).find("span[name='commentLikeQuantity']").text();
				$(this_).find("span[name='commentLikeQuantity']").text(parseInt(likeNum) + 1);
			} else {
				layer.msg('点赞失败',{time:1000});
			}
		},
		error : function() {
			layer.msg('点赞失败',{time:1000});
		}
	});
}


$(function() {
//	$('.boxs2_2 .right b a').click(function() {
//		$(this).toggleClass('current');
//	});
	$('.morepart a').click(function() {
		$(this).toggleClass('current');
		$(this).parent().parent().find('dl').toggleClass('current');
	});
//	$('#commentbtn,#add_comment a').click(function() {
//		$('#add_comment').toggle();
//	});
//	$('.replybtn,#reply_comment a').click(function() {
//		$('#reply_comment').toggle();
//	});
});